<?php

class Application_Model_CalendarEventsMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_CalendarEvents');
		}
		return $this->_dbTable;
	}
	
	public function insertAll(Application_Model_CalendarEvents $obj)
    {
        $data = array(
        	'member_id'	=> $obj->getMember_id(),
            'event_day'   => $obj->getEvent_day(),
            'event_month' => $obj->getEvent_month(),
        	'event_year'   => $obj->getEvent_year(),
        	'event_time'   => $obj->getEvent_time(),
            'event_title' => $obj->getEvent_title(),
        	'event_desc'   => $obj->getEvent_desc()
        );
        $this->getDbTable()->insert($data);
    }
    
	public function updateAll(Application_Model_CalendarEvents $obj)
    {
    	$db = $this->getDbTable();
    	
        $data = array(
        	'member_id'	=> $obj->getMember_id(),
        	'event_day'   => $obj->getEvent_day(),
            'event_month' => $obj->getEvent_month(),
        	'event_year'   => $obj->getEvent_year(),
        	'event_time'   => $obj->getEvent_time(),
            'event_title' => $obj->getEvent_title(),
        	'event_desc'   => $obj->getEvent_desc()
        );
        $db->update($data,array('event_id = ?' => $obj->getEvent_id()));
    }
    
	public function findByResult($select){
		$db = $this->getDbTable();
    	$resultSet = $db->fetchAll($select);
        $entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_CalendarEvents();
			$entry->setEvent_id($row->event_id)
				->setMember_id($row->member_id)
				->setEvent_day($row->event_day)
				->setEvent_month($row->event_month)
				->setEvent_year($row->event_year)
				->setEvent_time($row->event_time)
				->setEvent_title($row->event_title)
				->setEvent_desc($row->event_desc);
			$entries[] = $entry;
		}
		return $entries;
    }
    
	public function findAll(){
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_CalendarEvents();
			$entry->setEvent_id($row->event_id)
				->setMember_id($row->member_id)
				->setEvent_day($row->event_day)
				->setEvent_month($row->event_month)
				->setEvent_year($row->event_year)
				->setEvent_time($row->event_time)
				->setEvent_title($row->event_title)
				->setEvent_desc($row->event_desc);
			$entries[] = $entry;
		}
		return $entries;
	}
        
	public function findByMemberId($member_id, $month, $year)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('calendar_events')
	             ->where('member_id = ?', $member_id)
	             ->where('event_month = ?', $month)
	             ->where('event_year = ?', $year);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByMemberIdDate($member_id, $year, $month, $day)
    {
    	if($member_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('calendar_events')
	             ->where('member_id = ?', $member_id)
	             ->where('event_year >= ?', $year)
	             ->where('event_month >= ?', $month)
	             ->where('event_day >= ?', $day);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByEventId($event_id)
    {
    	if($event_id!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('calendar_events')
	             ->where('event_id = ?', $event_id);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByCountry($country)
    {
    	if($country!=null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('calendar_events')
	             ->where('event_desc = ?', $country)
	             ->group('member_id')
	             ->order( array( 'event_year DESC', 'event_month DESC', 'event_day DESC', 'event_time DESC' ) )
	             ->limitPage( 1, 10 );
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function deleteByEid($event_id)
    {
        $this->getDbTable()->delete(array(
        		'event_id = ?' => $event_id
        ));
    }
}

